package com.example.lotterysystem.common.interceptor;

import com.example.lotterysystem.common.utils.JWTUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.SpringApplicationEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @auther falls_vc
 * description:
 * @date 2025/5/20  9:09
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

    private static final Logger logger= LoggerFactory.getLogger(LoginInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取token
        String token = request.getHeader("user_token");
        logger.info("获取token：{}",token);
        logger.info("获取url；{}",request.getRequestURI());

        // 解析token
        Claims claims= JWTUtil.parseJWT(token);
        if(null == claims) {
            logger.error("JWT令牌解析失败，用户未登录！");
            response.setStatus(401);
            return false;
        }

        logger.info("JWT令牌解析成功，放行");
        return true;


    }
}
